package poem_info2.Task;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import poem_info2.JdbcUtils.JDBCUtils;
import poem_info2.JdbcUtils.ParseDetailedUrl2;
import poem_info2.domain.Id;
import poem_info2.domain.Poem;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * https://so.gushiwen.cn/guwen/
 * 古诗文网 爬取古籍
 */
public class Task2 {
    public static int count = 1;
    public static void main(String[] args) throws Exception {
        //存储每一次的poem对象
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        List<Poem> ps = template.query("select * from poems", new BeanPropertyRowMapper<Poem>(Poem.class));
        //存储url
        List<String> url_list = new ArrayList<String>();
        for (int i = 141; i <= 366; i++) {
            String href = "https://so.gushiwen.cn//guwen/book_" + i + ".aspx";
            url_list.add(href);
        }
        for (String url : url_list) {
            //获取每个类别诗歌的url页面，里面包含了每首诗歌的详情页url
            System.out.println("-------------------------------------------------分割线-----------------------------------------------------");
            System.out.println(url);
            //解析详情页url
            Document document = Jsoup.parse(new URL(url), 10000);
            //获取span
            Elements ele1 = document.select("div[style] > span");
            //如果ele1为空，说明query语句异常
            if (ele1.isEmpty()) {
                ele1 = document.select("div.bookcont > ul > span");
            }
            //古籍type
            String type = document.select("#sonsyuanwen > div.cont > h1 > span:nth-child(1) > b").text();
            //获取作者id
            Id id = new Id();
            Integer writer_id = id.getId(type, 1);
            //创建ParseDetailedUrl对象
            ParseDetailedUrl2 pdUrl = new ParseDetailedUrl2();
            for (Element element1 : ele1) {
                //创建poem对象
                Poem p = new Poem();
                //设置writer_id
                p.setWriter_id(writer_id);
                //每篇古籍url
                String href1 = "";
                href1 = element1.select("a").attr("href");
                //每篇古籍的标题
                String title = type + "-" + element1.select("a").text();
                p.setTitle(title);
                pdUrl.parseUrl(href1, ps, p);
            }
            System.out.println("-------------------------------------------------分割线-----------------------------------------------------");
        }
    }
}
